#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年6月12日

@author: chenyongbing
'''
import sys,os,commands
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append('../')
from base.retail_datas import myRetailDatas
from base.time_method import myTimeMethod
import logging
class UpdateHiveRoleData(object):
    '''
    classdocs
    '''


    def __init__(self,rolePath='',rerunRolePath='',newRolePath='',hiveTab='',update=False,merger=False):
        '''
        Constructor
        '''
        self.rolePath = rolePath
        self.rerunRolePath = rerunRolePath
        self.newRolePath = newRolePath
        self.hiveTab = hiveTab
        self.update = update
        self.merger = merger
    def delete_from_role_data(self,day,groups=[]):
        rolefile = self.rolePath+'/'+day+'/part-00000'
        
        newrolefile = self.newRolePath +'/'+day+'/part-00000'
        
        if os.path.exists(newrolefile):
            roledatas = open(newrolefile).read()
        elif  os.path.exists(rolefile):
            roledatas = open(rolefile).read()
        else:  
            logging.error('role file %s not exists.'%rolefile)
            sys.exit()
            
            
            
        
        
        newroledatas = ''
        
        for roleline in roledatas.split('\n'):
            #2015-06-11,32014527,b8:09:8a:69:2a:40,2015-06-11 10:30:39,2015-06-11 10:39:22,523000,3,Apple
            roleline_list = roleline.split(',')
            if len(roleline_list)!=8:continue
            group = roleline_list[1]
            if group in groups:continue
            newroledatas+='%s\n'%roleline
        return newroledatas
    
    
    def merger_role_data(self,day,groups=[]):
        rerunrolefile = self.rerunRolePath+'/'+day+'/part-00000'
        if not os.path.exists(rerunrolefile):
            logging.error('role file %s not exists.'%rerunrolefile)
            sys.exit()
        
        newroledatas = self.delete_from_role_data(day, groups)
        rerunroledata = open(rerunrolefile).read()
        
        roledatas = newroledatas+rerunroledata
        
        newrolefile = self.newRolePath+'/'+day+'/part-00000'
        if not os.path.exists(self.newRolePath+'/'+day):
            commands.getstatusoutput('mkdir -p %s'%(self.newRolePath+'/'+day))
        
        fr = open(newrolefile,'w')
        fr.write(roledatas)
        fr.close()
        
            
        
        
        
        
        
        
    def update_hive_role_data(self,startTime,endTime,groups=[]):
        days = myTimeMethod.get_slist_between_st_et(startTime, endTime)
        for day in days:
            newrolepath = self.newRolePath+'/'+day+'/'
            if self.merger==True:
                self.merger_role_data(day, groups)
                
#             if os.path.exists('%s/part-00000'%newrolepath)  and self.update:
#                 pass
#             else:
#                 self.merger_role_data(day, groups)
                
                logging.info('day %s merger OK'%day)
                print 'day %s merger OK'%day
            
            
            sql = 'LOAD DATA LOCAL INPATH "%s" OVERWRITE INTO TABLE %s PARTITION(d="%s")'%(newrolepath,self.hiveTab,day)
            cmd = "%s/hive -e '%s'"%(hivePath,sql)
            
            
            if  self.update==True:
                print cmd
                logging.info('hive:%s'%cmd)
                commands.getstatusoutput(cmd)
            
        
        
    
if __name__ == '__main__':
    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--startTime',metavar=u"starttime",default=None)
    parser.add_argument('--endTime',metavar=u"endtime",default=None)
    parser.add_argument('--comp_ids',metavar=u"comp_id",default=None)
    parser.add_argument('--update',action='store_true',help='update hive')
    parser.add_argument('--merger',action='store_true',help='merger hive data')
    #parser.add_argument('--update',action='store_true',help='update hive')
    args = parser.parse_args()
    startTime = args.startTime
    endTime = args.endTime
    comp_ids = args.comp_ids
    update = args.update
    merger = args.merger

    groups = []
    if comp_ids!=None:
        for comp_id in comp_ids.split(','):
            comp_group  = myRetailDatas.get_comp_group(comp_id=comp_id)
            
            ogroups = myRetailDatas.get_groups_by_comp( comp_id=comp_id)
            
            for g in ogroups:
                groups.append(str(g))
        
    rolePath='/data/java/data/new_role/'
    rerunRolePath='/data/rerun/data/new_role_test/'
    #newRolePath='/data/rerun/data/recover_role/'
    newRolePath = rolePath
    hiveTab = 'newhivehistoryrole'
    hivePath = '/data/ZmHadoop/apache-hive-0.13.0-bin/bin/'
    
    myUpdateHiveRoleData = UpdateHiveRoleData(rolePath=rolePath,rerunRolePath=rerunRolePath,newRolePath=newRolePath,hiveTab=hiveTab,update=update,merger=merger)
    myUpdateHiveRoleData.update_hive_role_data(startTime, endTime, groups)
    